<?php
/**
 * Created by PhpStorm.
 * User: tjjs
 * Date: 17-6-19
 * Time: 下午3:48
 */

namespace ActionsHttp;

use Data\Tree_customer_action_log;
use Data\Tree_customer_action_log_DAO;
use Data\Tree_productor_DAO;
use Data\Tree_gg_DAO;
use Data\Tree_species_DAO;

class ProductorInfo extends BaseActionHttp
{
    public function doAction()
    {
        parent::doAction();
        $customer_id_random_str = $this->data[CUSTOMER_ID];
        $customer_id = substr($customer_id_random_str,10);
        // 检测登陆是否过期
        if (!($this->checkSession($customer_id))) {
            return;
        }

        $group_id = $this->data[GROUP_ID];
        $province = $this->data[PROVINCE];
        $city = $this->data[CITY];
        $county = $this->data[COUNTY];
        $gg_cs = $this->data[GG_CS];

        if (!$province || !$city || !$county) {
            return $this->SendErrMsg("参数错误，地区请选择到区");
        }
        if (!$gg_cs) {
            return $this->SendErrMsg("参数错误，请选择规格参数");
        }
        if (!$group_id) {
            return $this->SendErrMsg("参数错误，请选择分组");
        }

        $productor_record = Tree_productor_DAO::getRecordByAreaGroupidGgcs($group_id,$province,$city,$county,$gg_cs);
        $productor_info = array();
        if ($productor_record) {
            $productor_info = $productor_record->getArray();
            $species_id = $productor_record->getSPECIES_ID();
            $species_record = Tree_species_DAO::getRecord($species_id);
            $dw = $species_record->getDW();

            $gg_id = $productor_record->getGG_ID();
            $gg_record = Tree_gg_DAO::getRecord($gg_id);
            $gg_name = $gg_record->getGG_NAME();
            $gg_dw = $gg_record->getGG_DW();
            $productor_info[GG_NAME] = $gg_name;
            $productor_info[GG_DW] = $gg_dw;
            $productor_info[DW] = $dw;
            // 本月询价次数
            $visit_count = $productor_record->getVISIT_COUNT()+1;
            $productor_record->setVISIT_COUNT($visit_count);
            Tree_productor_DAO::update($productor_record);

            $productor_id = $productor_record->getPRODUCTOR_ID();
            // 询价记录
            $customer_action_log_record = new Tree_customer_action_log(array());
            $customer_action_log_record->setCUSTOMER_ID($customer_id);
            $customer_action_log_record->setPRODUCTOR_ID($productor_id);
            $customer_action_log_record->setDATE(date("Y-m"));
            Tree_customer_action_log_DAO::insert($customer_action_log_record);
        }

        // 返回数据
        $this->setValue(PRODUCTOR_INFO,$productor_info);
        $this->ResponseDataToCurrentClient();
    }
}